package gqz.testdemo.Bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.Set;

/**
 * Created by Administrator on 2018/8/28.
 * 蓝牙工具类
 */

public class BTUtils {

    private final String TAG = "BTUtils-->";

    private Context context;

    private BluetoothManager bm;
    private BluetoothAdapter ba;
    private BluetoothDevice connDev;

    private BluetoothGatt mGatt;
    private BluetoothLeScanner scan;

    public BTUtils(Context context) {
        this.context = context;

        ba = BluetoothAdapter.getDefaultAdapter();
        scan = ba.getBluetoothLeScanner();
    }

    public IntentFilter getReceiverFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙状态关闭
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//设备配对状态
        filter.addAction(BluetoothDevice.ACTION_FOUND);//设备查找
        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        return filter;
    }

    private BluetoothAdapter getBluetoothAdapter() {
        BluetoothManager bm = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        return bm.getAdapter();
    }

    //打开蓝牙
    public void openBluetooth(){

    }

    //

    //获取本机蓝牙名称
    public String getBleName() {
        return ba.getName();
    }

    //设置本机蓝牙名称
    public void setBleName(String name) {
        ba.setName(name);
    }

    //获取配对过的设备
    public Set<BluetoothDevice> getBondDev(){
        return ba.getBondedDevices();
    }

    //获取允许检测状态
    public boolean getAllowSearchStatus() {
        int mode = ba.getScanMode();
        Log.i(TAG, "getAllowSearchStatus: scanMode=" + mode);
        switch (mode) {
            case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
                Log.i(TAG, "getAllowSearchStatus: 不可见");
//                switch_search.setChecked(false);
                break;
            case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
                Log.i(TAG, "getAllowSearchStatus: 可见");
//                switch_search.setChecked(true);
                return true;
        }
        return false;
    }

    //开启允许检测
    public void openAllowSearch() {
        //启动修改蓝牙可见性的Intent
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        //设置蓝牙可见性的时间，方法本身规定最多可见300秒
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        context.startActivity(intent);
    }

    //关闭允许检测
    public void closeAllowSearch() {
        try {
            Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
            setDiscoverableTimeout.setAccessible(true);
            Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class);
            setScanMode.setAccessible(true);

            setDiscoverableTimeout.invoke(ba, 1);
            setScanMode.invoke(ba, BluetoothAdapter.SCAN_MODE_CONNECTABLE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
